<template>
  <div>
    <van-dropdown-menu>
      <van-dropdown-item disabled title="综合" v-model="priceVal" />
      <van-dropdown-item
        title="价格"
        v-model="priceVal"
        :options="option2"
        @change="priceChange"
      />
      <van-dropdown-item
        title="分类"
        v-model="categoryVal"
        :options="filterCategory"
        @change="categoryChange"
      />
    </van-dropdown-menu>
    <!-- 下面两个组件不共存 -->
    <van-empty
      v-if="goodsList.length == 0"
      image="search"
      description="商品未上架"
    />
    <!-- 单独抽离产品组件 -->
    <Products v-else :goodsList="goodsList"></Products>
  </div>
</template>

<script>
import Products from "./Products.vue";
export default {
  name: "SearchProducts",
  props: ["filterCategory", "goodsList"],
  components: { Products },
  data() {
    return {
      priceVal: "",
      categoryVal: "",
      option2: [
        { text: "价格由高到低", value: "desc" },
        { text: "价格由低到高", value: "asc" },
      ],
    };
  },
  beforeUpdate() {
    // 数据挂载完毕， 遍历filterCategory数组，看看哪一项的checked为true，
    // 遍历的时候 遇到checked为true时，就把  this.categoryVal 改成它的value值，这样这一项就被勾上了
    this.filterCategory.map((item) => {
      // 如果item的checked为true，就改this.categoryVal
      if (item.checked) {
        this.categoryVal = item.value;
        return;
      }
    });
  },
  methods: {
    // 点击种类修改页面展示的商品数据列表
    categoryChange(val) {
      this.$emit("categoryChange", val);
    },

    // 点击价格修改页面的商品数据列表
    priceChange(val) {
      this.$emit("priceChange", val);
    },
  },
};
</script>

<style>
</style>